﻿using famails.App_Code.source.dto;
using famails.bus;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.Discount
{
    public partial class DiscountList : System.Web.UI.Page
    {
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
            }
        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");
            return null;
        }
        
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<CM_DiscountDTO> Search(string name, string code, string from, string to)
        {
            userLogin = (UserLoginDTO)HttpContext.Current.Session["us-login"];
            List<CM_DiscountDTO> result = new List<CM_DiscountDTO>();
            try
            {
                //string name = "", phone = "h", email = ""; int group = -1;
                CM_DiscountBUS ctBUS = new CM_DiscountBUS();
                DataTable DiscountBySelect = ctBUS.Search(name, code, userLogin.UserId, from, to);
                CM_DiscountDTO item = new CM_DiscountDTO();
                foreach (DataRow dr in DiscountBySelect.Rows)
                {
                    item = new CM_DiscountDTO();
                    item.DiscountID = Convert.ToInt32(dr["DiscountID"] + "");
                    item.DiscountName = dr["DiscountName"] + "";
                    item.DiscountValue = Convert.ToDecimal(dr["DiscountValue"] + "");
                    item.MinAmount = Convert.ToDecimal(dr["MinAmount"] + "");
                    item.Notes = dr["Notes"] + "";
                    if (dr["FromDate"] + "" != "")
                        item.FromDate = Convert.ToDateTime(dr["FromDate"] + "");
                    if (dr["ToDate"] + "" != "")
                        item.ToDate = Convert.ToDateTime(dr["ToDate"] + "");
                    if (dr["CreateDate"] + "" != "")
                        item.CreateDate = Convert.ToDateTime(dr["CreateDate"] + "");
                    item.NumberOfUse = Convert.ToInt32(dr["NumberOfUse"]);
                    item.NumberOfCode = Convert.ToInt32(dr["NumberOfCode"]);
                    item.TotalValue = Convert.ToInt32(dr["TotalValue"]);
                    item.DiscountType = dr["Fix0_Per1"] + "" == "0" ? "Số tiền" : "Phần trăm";
                    result.Add(item);
                }
            }
            catch (Exception ex)
            {
               common.Common.logs("Discount - Search", ex, common.Common.logType.error);
            }
            return result;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Delete(int id)
        {
            try
            {
                CM_DiscountBUS ctBUS = new CM_DiscountBUS();
                if (!ctBUS.CheckBeforeDelete(id))
                    ctBUS.Delete(id);
                else throw new Exception("Khách hàng này đã mua hàng, bạn không thể xóa!");
            }
            catch (Exception ex)
            {
                common.Common.logs("Discount - Delete", ex, common.Common.logType.error);
                throw ex;
            }
        }
    }
}